"""
所谓 代理池，就是使用 random() 然后将 多个代理IP 来随机 抽取加载到 ProxyHandler() 中
"""

import urllib.request

# 代理池
# 需要能用的 IP
proxy_pool = [
	{'http': '36.248.132.103:9999'},
	{'http': '171.35.214.9:9999'},
	{'http': '123.101.231.240:9999'}
]

import random

# 随机抽取 代理IP
proxies = random.choice(proxy_pool)

url = 'http://www.baidu.com/s?wd=ip'

headers = {
	'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
}

request = urllib.request.Request(url=url, headers=headers)

handler = urllib.request.ProxyHandler(proxies=proxies)
opener = urllib.request.build_opener(handler)
response = opener.open(request)

resp_data = response.read().decode('utf-8')

with open('代理池.html', 'w', encoding='utf-8') as file:
	file.write(resp_data)


